const app = getApp()
const $api = require('../../utils/api.js').API;
var login = require('../../utils/login.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({
    data: {
        info:{},
        data:{},
        ruleShow:false,
        yet:'',
        seeNum:0,
        subscribe:false,
        CompanyStatus:1,
        viewId:0,
        takeShow:false,
        takeMoney:0,
        takeTrue:true,
        role:0
    },
    onShow: function(){
        //wx.setStorageSync('uid',12)
        //wx.setStorageSync('role',4)
        this.setData({
            role:wx.getStorageSync('role')
        })
        if(!wx.getStorageSync('uid')){
            //没有缓存 跳转授权页面
            wx.navigateTo({
                url:'/pages/index/guide'
            })
        }else{
            //验证手机号码是否存在
            this.getUserInfo();
        }
    },
    getYet:function(){
        var data = {'uid':wx.getStorageSync('uid')}
        var that = this
        $api.get('money/yet',data).then(res => {
            //请求成功
            if(res.data.code == 1){
              that.setData({
                yet:res.data.data
              })
            }
        })
    },
    getUserInfo:function(){
        var data = {
            'uid':wx.getStorageSync('uid')
        }
        var that = this
        $api.get('member/info',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                if(!res.data.data.phone){
                    wx.navigateTo({
                        url:'/pages/index/phone'
                    })
                    return;
                }
                that.setData({
                    data:res.data.data
                })
                that.getYet();
                that.getWx()
                that.viewSave();
                if(wx.getStorageSync('role') == 3){
                    that.getCompanyStatus()
                    that.getSeeNum();
                }
            }
        })
    },
    cancelPhone:function(){
        this.setData({
            rcodeShow:false
        })
    },
    rulesee:function(){
        var data = {'id':1}
        var that = this
        $api.get('rule/info',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                var article = res.data.data.content;
                WxParse.wxParse('article', 'html', article,that, 5,that.data.imgUrl);
                // that.setData({
                //     info:res.data.data
                // })
                // that.setData({
                //     data:res.data.data
                // })
            }
        })
        this.setData({
            ruleShow:true
        })
    },
    cancelRule:function(){
        this.setData({
            ruleShow:false,
        })
    },
    jump:function(e){
        var name = e.currentTarget.dataset.name;
        //个人中心--企业中心
        if(name == 'mycenter'){
            //属于个人用户  可注册状态
            if(this.data.role == 1){
                wx.navigateTo({
                    url:'/pages/login/index'
                })
                return;
            }
            var data = {
                uid:wx.getStorageSync('uid'),
                role:wx.getStorageSync('role')
            }


            var status = true
            $api.get('user/getInfoStatus',data).then(res => {
                //请求成功
                if(res.data.code == 1){
                    var status = true
                }else{
                    var status = false
                }
                if(status){
                    if(this.data.role == 2){
                        var url = '/pages/user/user/index'
                    }
                    if(this.data.role == 3){
                        var url = '/pages/user/company/index'
                    }
                }else{
                    if(this.data.role == 2){
                        var url = '/pages/login/personal'
                    }
                    if(this.data.role == 3){
                        var url = '/pages/login/company'
                    }
                }
                wx.navigateTo({
                    url:url
                })
            })
            return;
        }
        wx.navigateTo({
            url:name
        })
    },
    onShareAppMessage: function() {
        return {
            title: "天意美业直通车",
            path: "/pages/index/index?puid="+wx.getStorageSync('uid')
        };
    },
    getSeeNum:function(){
        var data ={'uid':wx.getStorageSync('uid')}
        var that = this
        $api.get('vip/seeNum',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                that.setData({
                    seeNum:res.data.data
                })
            }
        })
    },
    getWx:function(){
        var data = {'uid':wx.getStorageSync('uid')}
        var that = this
        $api.get('user/wxUser',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                if(res.data.data.subscribe == 2){
                    that.setData({
                        subscribe:true
                    })
                }
            }else{
              that.setData({
                subscribe:true
              })
            }
        })
    },
    getCompanyStatus:function(){
        var data = {'uid':wx.getStorageSync('uid')}
        var that = this//company/companyInfo
        $api.get('company/companyInfo',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                that.setData({
                    CompanyStatus:res.data.data.is_lease
                })
            }
        })
    },
    lease:function(e){
        var is_lease = e.currentTarget.dataset.type
        var data = {
            is_lease:is_lease,
            uid:wx.getStorageSync('uid')
        }
        var that = this
        $api.put('company/putLease',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                that.setData({
                    CompanyStatus:is_lease
                })
                wx.showToast({
                    title:'成功',
                    duration:2000,//显示时长
                    mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                    icon:'none', //图标，支持"success"、"loading" 
                })
            }
        })
    },
    viewSave:function(){
        var that = this
        var data = {
            uid:wx.getStorageSync('uid')
        }
        $api.post('view/add',data).then(res => {
            if(res.data.code == 1){
                that.setData({
                    viewId:res.data.data
                })
            }
        })
    },
    onHide:function(){
        var that = this
        var data = {id:this.data.viewId}
        $api.put('view/put',data).then(res => {
            
        })
    },
    take:function(){
        if(this.data.subscribe){
            wx.navigateTo({
                url:'/pages/user/wxRcode/index'
            })
        }else{
            this.setData({
                takeShow:true
            })
        }
    },
    takeClose:function(){
        this.setData({
            takeShow:false
        })
    },
    takeInput:function(e){
        this.setData({
            takeMoney:e.detail.value
        })
    },
    takeAdd:function(){
        this.setData({
            takeTrue:false
        })
        if(parseFloat(this.data.takeMoney) > this.data.data.money){
            wx.showToast({
                title:'余额不足',
                duration:2000,//显示时长
                mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon:'none', //图标，支持"success"、"loading" 
            })
            return;
        }
        if(parseFloat(this.data.takeMoney) > 200){
            wx.showToast({
                title:'超过单日提现限额200',
                duration:2000,//显示时长
                mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon:'none', //图标，支持"success"、"loading" 
            })
            return;
        }
        var data = {
            'uid':wx.getStorageSync('uid'),
            'money':this.data.takeMoney
        }
        var that = this
        $api.post('take/add',data).then(res => {
            that.setData({
                takeTrue:true
            })
            //请求成功
            if(res.data.code == 1){
                wx.showToast({
                    title:'申请成功',
                    duration:2000,//显示时长
                    mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                    icon:'none', //图标，支持"success"、"loading" 
                })
                var data = that.data.data
                data.money = data.money - this.data.takeMoney
                that.setData({
                    data:data,
                    takeShow:false
                })
            }else{
                wx.showToast({
                    title:res.data.msg,
                    duration:2000,//显示时长
                    mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                    icon:'none', //图标，支持"success"、"loading" 
                })
            }
        })
    },
    buy:function(){
        //查看当前机构是否购买过  有群发消息的权限
        var that = this
        var data = {
            company_uid:wx.getStorageSync('uid')
        }
        $api.get('group/num',data).then(res => {
            if(res.data.data){
                wx.navigateTo({
                    url:'/pages/user/message?num='+res.data.data.num+'&groupId='+res.data.data.id
                })
            }else{
                wx.navigateTo({
                    url:'/pages/user/group'
                })
            }
        })
    },
    buy1:function(){
        //查看当前机构是否购买过  有群发消息的权限
        var that = this
        var data = {
            uid:wx.getStorageSync('uid')
        }
        $api.get('vip/message',data).then(res => {
            if(res.data.code == 1){
                wx.navigateTo({
                    url:'/pages/user/push'
                })
            }else{
                wx.showToast({
                    title:'白金VIP会员有此权限',
                    duration:2000,//显示时长
                    mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                    icon:'none', //图标，支持"success"、"loading" 
                })
                return;
            }
        })
    }
});